#include "StdAfx.h"
#include "JsEngine.h"

using namespace MSScriptControl;

CJsEngine::CJsEngine(void)
: COleClass()
, m_pScriptControl(__uuidof(ScriptControl))
{
	m_pScriptControl->PutLanguage(_T("JavaScript"));
	m_pScriptControl->PutAllowUI(FALSE);
 	m_pScriptControl->AddCode(_T("function GetJsonVal(json, val){return json[val];}")); 
}

CJsEngine::~CJsEngine(void)
{
}

int CJsEngine::GetJsonInt(LPCTSTR lpszJson, LPCTSTR lpszKey)
{
	CString csJs;
	CString csJson;
	CString csKey;
	VARIANT vRet;
	int nRet = 0;

 	csJson = lpszJson;
	csKey = lpszKey;
	csKey.Replace(_T("\\"), _T("\\\\"));
	csKey.Replace(_T("\""), _T("\\\""));
	csJs.Format(_T("GetJsonVal(%s,\"%s\");"), csJson, csKey);

	vRet = m_pScriptControl->Eval((LPCTSTR)csJs); 

	switch (vRet.vt)
	{
	case VT_EMPTY:
		break;
	case VT_I1:
		nRet = vRet.cVal;
		break;
	case VT_I2:
		nRet = vRet.iVal;
		break;
	case VT_I4:
		nRet = vRet.lVal;
		break;
	case VT_INT:
		nRet = vRet.intVal;
		break;
	case VT_UI1:
		nRet = vRet.bVal;
		break;
	case VT_UI2:
		nRet = vRet.uiVal;
		break;
	case VT_UI4:
		nRet = vRet.ulVal;
		break;
	case VT_UINT:
		nRet = vRet.uintVal;
		break;
	default:
		break;
	}

	return nRet;
}

unsigned int CJsEngine::GetJsonUint(LPCTSTR lpszJson, LPCTSTR lpszKey)
{
	CString csJs;
	CString csJson;
	CString csKey;
	VARIANT vRet;
	unsigned int uRet = 0;

	csJson = lpszJson;
	csKey = lpszKey;
	csKey.Replace(_T("\\"), _T("\\\\"));
	csKey.Replace(_T("\""), _T("\\\""));
	csJs.Format(_T("GetJsonVal(%s,\"%s\");"), csJson, csKey);

	vRet = m_pScriptControl->Eval((LPCTSTR)csJs); 

	switch (vRet.vt)
	{
	case VT_EMPTY:
		break;
	case VT_I1:
		uRet = (BYTE)vRet.cVal;
		break;
	case VT_I2:
		uRet = (USHORT)vRet.iVal;
		break;
	case VT_I4:
		uRet = (ULONG)vRet.lVal;
		break;
	case VT_INT:
		uRet = (UINT)vRet.intVal;
		break;
	case VT_UI1:
		uRet = vRet.bVal;
		break;
	case VT_UI2:
		uRet = vRet.uiVal;
		break;
	case VT_UI4:
		uRet = vRet.ulVal;
		break;
	case VT_UINT:
		uRet = vRet.uintVal;
		break;
	default:
		break;
	}

	return uRet;
}

CString CJsEngine::GetJsonStr(LPCTSTR lpszJson, LPCTSTR lpszKey)
{
	CString csJs;
	CString csJson;
	CString csKey;
	VARIANT vRet;
	CString csRet;

	csJson = lpszJson;
	csKey = lpszKey;
	csKey.Replace(_T("\\"), _T("\\\\"));
	csKey.Replace(_T("\""), _T("\\\""));
	csJs.Format(_T("GetJsonVal(%s,\"%s\");"), csJson, csKey);

	vRet = m_pScriptControl->Eval((LPCTSTR)csJs); 

	switch (vRet.vt)
	{
	case VT_EMPTY:
		break;
	case VT_I1:
		csRet.Format(_T("%d"), vRet.cVal);
		break;
	case VT_I2:
		csRet.Format(_T("%d"), vRet.iVal);
		break;
	case VT_I4:
		csRet.Format(_T("%d"), vRet.lVal);
		break;
	case VT_INT:
		csRet.Format(_T("%d"), vRet.intVal);
		break;
	case VT_UI1:
		csRet.Format(_T("%u"), vRet.bVal);
		break;
	case VT_UI2:
		csRet.Format(_T("%u"), vRet.uiVal);
		break;
	case VT_UI4:
		csRet.Format(_T("%u"), vRet.ulVal);
		break;
	case VT_UINT:
		csRet.Format(_T("%u"), vRet.uintVal);
		break;
	case VT_BSTR:
		csRet = vRet.bstrVal;
	default:
		break;
	}

	return csRet;
}
